local youbai = fk.CreateSkill{
  name = "xiaobai__youbai",
}

youbai:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  derived_piles = "dengai_field",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:usedSkillTimes(youbai.name) == 0 then
      for _, move in ipairs(data) do
        if move.from == player and (move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResponse) then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerHand) then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {skill_name = youbai.name}) then
      local targets = room:getAlivePlayers()
      local maxNum,maxEquip = 0,0
      for _, p in ipairs(targets) do
        maxNum = math.max(maxNum, p:getHandcardNum())
        maxEquip = math.max(maxEquip,#p:getCardIds("e"))
      end
      local handcard_max = table.filter(targets, function(p) return p:getHandcardNum() == maxNum end)
      local equip_max = table.filter(targets, function(p) return #p:getCardIds("e") == maxEquip end)
      
      local equip_target = room:askToChoosePlayers(player, {
        targets = equip_max,
        min_num = 1,
        max_num = 1,
        prompt = "#youbai-equip",
        skill_name = youbai.name
      })
      local handcard_target
      if equip_target then
        handcard_target = room:askToChoosePlayers(player,{
          targets = handcard_max,
          min_num = 1,
          max_num = 1,
          prompt = "#youbai-handcard",
          skill_name = youbai.name})
        if #handcard_target > 0 then
          event:setCostData(self, {equip_target = equip_target, handcard_target = handcard_target})
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    
    cost_data.equip_target[1]:drawCards(1,youbai.name)
    cost_data.handcard_target[1]:drawCards(1,youbai.name)
    if player.dead then return end

    if cost_data.equip_target[1] ~= cost_data.handcard_target[1] then
      local targets = room:askToChooseToMoveCardInBoard(player, {
        prompt = "#youbai-move",
        skill_name = youbai.name,
        cancelable = true
      })
      if #targets ~= 0 then 
        room:askToMoveCardInBoard(player, {
          target_one = targets[1],
          target_two = targets[2],
          skill_name = youbai.name
        })
      end
    end
  end,

})

Fk:loadTranslationTable{
  ["xiaobai__youbai"] = "游摆",
  ["#youbai-equip"] = "选择一名装备区牌数最多的角色",
  ["#youbai-handcard"] = "选择一名手牌数最多的角色",
  ["#youbai-move"] = "你可以移动场上一张牌",
  [":xiaobai__youbai"] = "每回合限一次，你不因使用或打出失去手牌后，你可以依次选择一名装备区牌数最多的角色与一名手牌数最多的角色各摸一张牌。若两者为不同角色，则你可以移动场上一张牌。",

  ["$xiaobai__youbai1"] = "鼎秤易易，游于中庸可得万全。",
  ["$xiaobai__youbai2"] = "昭伯武威号施天下，仲达才略定策千军。",
}

return youbai